package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public int save(Comment comment) {
        //1.在comment表中新增一条记录
        mongoTemplate.save(comment);

        //2.根据操作类型修改对应操作数量,更新到数据库中
        //2.1查询操作类型
        Integer commentType = comment.getCommentType();
        switch(commentType){
            //1.对动态点赞
            case 1:{
                //2.2获取操作内容的id,例如动态id,获取操作内容,例如动态对象
                ObjectId publishId = comment.getPublishId();
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.3修改对应操作的数量
                movement.setLikeCount(movement.getLikeCount()+1);
                //2.4更新到数据库
                mongoTemplate.save(movement);
                //2.5返回最新的操作数量
                return movement.getLikeCount();
            }
            //2.对动态评论
            case 2:{
                //2.2获取操作内容的id,例如动态id,获取操作内容,例如动态对象
                ObjectId publishId = comment.getPublishId();
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.3修改对应操作的数量
                movement.setCommentCount(movement.getLikeCount()+1);
                //2.4更新到数据库
                mongoTemplate.save(movement);
                //2.5返回最新的操作数量
                return movement.getCommentCount();
            }
            //3.对动态喜欢
            case 3:{
                //2.2获取操作内容的id,例如动态id,获取操作内容,例如动态对象
                ObjectId publishId = comment.getPublishId();
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.3修改对应操作的数量
                movement.setLoveCount(movement.getLikeCount()+1);
                //2.4更新到数据库
                mongoTemplate.save(movement);
                //2.5返回最新的操作数量
                return movement.getLoveCount();
            }
            //4.-对视频点赞  5.-对视频评论  6.-对评论
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }

    }


    @Override
    public int delete(Long userId, ObjectId publishId, Integer commentType) {
        //1.创建条件查询对象
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                        .and("userId").is(userId)
                        .and("commentType").is(commentType)
        );

        //2.从comment表中删除该条记录
        mongoTemplate.remove(query,Comment.class);

        //3.根据操作类型修改对应的操作数量
        switch(commentType){
            //1.对动态取消点赞
            case 1:{
                //3.1获取操作内容,例如动态对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //3.2修改对应操作的数量
                movement.setLikeCount(movement.getLikeCount()-1);
                //3.3更新到数据库
                mongoTemplate.save(movement);
                //3.4返回最新的操作数量
                return movement.getLikeCount();
            }
            //2.对动态删除评论
            case 2:{
                //3.1获取操作内容,例如动态对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //3.2修改对应操作的数量
                movement.setCommentCount(movement.getLikeCount()-1);
                //3.3更新到数据库
                mongoTemplate.save(movement);
                //3.4返回最新的操作数量
                return movement.getCommentCount();
            }
            //3.对动态取消喜欢
            case 3:{
                //3.1获取操作内容,例如动态对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //3.2修改对应操作的数量
                movement.setLoveCount(movement.getLikeCount()-1);
                //3.3更新到数据库
                mongoTemplate.save(movement);
                //3.4返回最新的操作数量
                return movement.getLoveCount();
            }
            //4.-对视频取消点赞  5.-对视频删除评论  6.-对评论删除
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }


    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer pageNum, Integer pageSize) {
        //1.创建查询对象
        Query query  = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(2));

        //2.查询comment表获取总记录数
        long count = mongoTemplate.count(query, Comment.class);

        //3.继续往查询对象上添加分页和排序条件
        query.with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //4.查询comment表获取当前页的数据
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //5.封装pageBeanVo奇瑞返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }


    @Override
    public PageBeanVo findByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1.创建查询对象(发布人id,类型)
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));

        //2.查询comment表总条数
        long count = mongoTemplate.count(query, Comment.class);

        //3.追加条件(分页,排序)
        query.with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //4.查询comment表
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //5.封装pageBeanVo返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }


}
